﻿using System.Collections.Generic;
using System;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;

namespace commonV2
{
    public class Networking 
    {
        //From using Networking toolkit from  http://mftoolkit.codeplex.com/releases/view/24469
        public static DateTime GetNetworkTime(string ntpServerAddress)
        {
            return MSchwarz.Net.Ntp.NtpClient.GetNetworkTime(ntpServerAddress);
        }

        public static string GetHostName()
        {
            return Dns.GetHostName();
        }
        public static string GetHostIP()
        {
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            if (ips != null && ips.Length > 0) return ips[0].ToString();
            return null;
        }
        public static string GetHostDomainName()
        {
            return IPGlobalProperties.GetIPGlobalProperties().DomainName;
        }
        public static string GetHostFQDN()
        {
            string domainName = IPGlobalProperties.GetIPGlobalProperties().DomainName;
            string hostName = Dns.GetHostName();
            if (!hostName.Contains(domainName))   return hostName + "." + domainName;
            return hostName;
        }
        public static string GetMAC()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            if (nics != null && nics.Length > 0)
            {
                PhysicalAddress address = nics[0].GetPhysicalAddress();
                byte[] bytes = address.GetAddressBytes();
                string mac = "";
                for (int idx = 0; idx < bytes.Length; idx++)
                {
                    mac += bytes[idx].ToString("X2");
                    if (idx != bytes.Length - 1) mac += ":";
                }
                return mac;
            }
            return null;
        }
    }
}